package com.jinqing.wuchuang.task;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import com.jinqing.wuchuang.entity.SettledPush;
import com.jinqing.wuchuang.service.PushService;
import com.jinqing.wuchuang.utils.JPushUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

@Component
@EnableScheduling  // 1.开启定时任务
@EnableAsync       // 2.开启多线程
public class PushTask {
    private static final Logger logger = LoggerFactory.getLogger(PushTask.class);

    @Autowired
    PushService pushService;
    @Async
//    @Scheduled(cron="0 0/1 * * * ?") //每分钟一次
    @Scheduled(cron="0 0 8 1/1 * ?") //每天八点
    public void paymentNotice(){
        JPushClient jPushClient = JPushUtil.getJPushClient();
        //更新别名
        // PushClient.updateDeviceTagAlias("170976fa8a449a3f808","0121911360000",null,null);
        Integer threeMonths = 3*30*24*60*60*1000;
        Integer oneWeek = 7*24*60*60*1000;
        //list中存放未按时缴费人员的UID
        List<String> noPay = new ArrayList<>();
        List<Long> deleteList = new ArrayList<>();
        // 1.查询所有用户
        List<SettledPush> users = pushService.selectAllUser();
        // 2.获取当前时间戳
        Long timeStamp = System.currentTimeMillis();
        logger.info("timestamp:" + timeStamp);
        for (SettledPush user : users) {
            // 3.如果未超过 3个月 且 isPay =`1,令isPay = 0
            if (timeStamp - user.getPayTime().getTime() < threeMonths && (user.isPay() == true)) {
                logger.info("更新缴费状态：" + user.toString());
                pushService.updatePayStatus(user);
            }// 4.超过 3个月 加 7天
            else if (timeStamp - user.getPayTime().getTime() > threeMonths + oneWeek && (user.isPay() == false)) {
                logger.info("未按时缴费用户：" + user.toString());
                deleteList.add(user.getUid());
            }// 5.超过 3个月 且 未达到最后期限 且 isPay = false,加入未按时缴费人员名单
            else if (timeStamp - user.getPayTime().getTime() > threeMonths && (user.isPay() == false)) {
                noPay.add(String.valueOf(user.getUid()));
            }

        }
        // 6.有未按时缴费的，发送通知
        if (noPay.size() != 0){
            logger.info("未按时缴费人员："+noPay.toString());
            try {
                JPushUtil.sendToAliasList(noPay,"缴费通知","一周内尽快交钱",null);
            } catch (APIConnectionException e) {
                logger.error("发生错误：",e);
            } catch (APIRequestException e) {
                logger.error("发生错误：",e);
            }
        }
        // 7.有不交钱的，该干嘛干嘛
        if(deleteList.size() != 0){
            for (Long uid:deleteList) {

            }
        }
    }
}
